using Game.Main.Model;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class CurtainItem : BaseItem
	{
		public enum Anim
		{
			Static = 0,
			Down = 1
		}

		private float UP_POSITION = 8.7f;

		private float DOWN_POSITION;

		private Anim anim;

		private float animTimer;

		private Transform curtain;

		private int curIndex;

		private int curtainAmount;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			curtain = model.Find("Curtain");
			curtainAmount = curtain.childCount;
			curtain.SetLocalZ(-1E-05f);
			SetCurtain(data.state);
		}

		protected override void LateUpdate()
		{
			if (anim != 0 && anim == Anim.Down)
			{
				animTimer += Time.deltaTime / 4f;
				curtain.SetLocalY(Mathf.Lerp(UP_POSITION, DOWN_POSITION, animTimer));
				if (animTimer >= 1f)
				{
					anim = Anim.Static;
					model.GetComponent<SpriteRenderer>().sprite = curtain.GetComponent<SpriteRenderer>().sprite;
					curtain.SetLocalY(UP_POSITION);
				}
			}
		}

		public void ChangeNextCurtain()
		{
			if (anim == Anim.Static)
			{
				curIndex++;
				if (curIndex >= curtainAmount)
				{
					curIndex = 0;
				}
				curtain.GetComponent<SpriteRenderer>().sprite = curtain.GetChild(curIndex).GetComponent<SpriteRenderer>().sprite;
				animTimer = 0f;
				anim = Anim.Down;
			}
		}

		private void SetCurtain(int index)
		{
			if (curIndex != index)
			{
				curIndex = index;
				if (curIndex >= curtainAmount)
				{
					curIndex = 0;
				}
				model.GetComponent<SpriteRenderer>().sprite = curtain.GetChild(curIndex).GetComponent<SpriteRenderer>().sprite;
			}
		}

		public bool CanChangeCurtain()
		{
			if (anim == Anim.Static)
			{
				return true;
			}
			return false;
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			if (curIndex != 0)
			{
				obj["33"] = curIndex;
			}
		}
	}
}
